#ifndef __FILESYSTEM_H__
#define __FILESYSTEM_H__

#include "include/IFileSystem.h"
#include "IOFile.h"
#include "LoadedImage.h"
#include "FileLoader.h"

typedef struct PFSFilePointer
{
	u32 Signature2;
	u32 FileLen;
	u32 FilePointer;
	char FileName[256];
	u32 IsDir;
} PFSFilePointer;

typedef struct PFSHeader
{
	u32 Signature1;
	u32 PFSHeaderLen;
	u32 TotalNodes;
} PFSHeader;

typedef struct AttachedFileSystemsStrc
{
	u8 *Data;
	stringc PFSFileName;
	bool Readed;
	u32 FilePointer;
} AttachedFileSystemsStrc;

typedef struct FileSearchingStrc
{
	stringc DirPFS;
	stringc PathInPFS;
	stringc FileOnly;
	u32 Index;
} FileSearchingStrc;

#define PFS_FILEONDISK_EXT		".pfs"
#define PFS_FILEONMEM_EXT		".hhh"

class CFileSystem : public IFileSystem
{
protected:
	stringc WorkingDirectory;
	CIOFile *Base;
	bool AutomaticLoading;
	CFileLoader fl;

	std::vector<AttachedFileSystemsStrc*> AttachedFileSystems;
	u32 ActualAttachedReading;
	std::vector<FileSearchingStrc*> FileSearching;

public:
	PFSHeader header;
	std::vector<PFSFilePointer*> fpvec;

public:
	CFileSystem();
	virtual ~CFileSystem();

	bool SetWorkingDirectoryToStartDirectory();
	bool ReadActualDirectory();
	stringc GetWorkingDirectory();

	CIOFile *GetNewCIOFile(stringc Dir,stringc File);

	IImage *GetImage(stringc Name);
	CLoadedImage *GetLoadedImage(stringc Name);

	IIOFile *GetFile(stringc Name);
	IIOFile *GetBaseFile();
	bool ReadAttachedFileSystem(u8 *Data,stringc FileName);
	std::vector<AttachedFileSystemsStrc*> &GetAttachedFileSystems();
	void IncActualAttachedReading();
	std::vector<FileSearchingStrc*> &GetFileSearching();
};

#endif
